package com.jingyanzi.privilege.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.google.gson.Gson;

import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

import com.jingyanzi.privilege.domain.BtnDO;

public class Test {

	public static void main(String[] rags) throws Exception {
		/*Gson gson = new Gson();
		BufferedReader jsonIn = new BufferedReader(new InputStreamReader(new FileInputStream("d:/btn.json"), "UTF-8"),
				50 * 1024 * 1024);
		JsonParser parser = new JsonParser();
		String json = new String(parser.parse(jsonIn).toString().getBytes(), "utf-8");
		List<BtnDO> btnsDO = gson.fromJson(json, new TypeToken<List<BtnDO>>() {
		}.getType());

		for (BtnDO b : btnsDO) {
			insert(b);
		}
		
		 * for(int i = 0 ; i < 100; i++) { int id = UUID.randomUUID().hashCode(); id =
		 * Math.abs(id); System.out.println(id); }
		 */
		
		String filename = "btn.json";
		
		String[] names = filename.split(",");
		for(String name : names) {
			System.out.println(name);
		}
	}

	private static Connection getConn() {
		String driver = "com.mysql.jdbc.Driver";
		String url = "jdbc:mysql://192.168.50.195:3306/sysSecurity";
		String username = "root";
		String password = "Lg@5400713";
		Connection conn = null;
		try {
			Class.forName(driver); // classLoader,加载对应驱动
			conn = (Connection) DriverManager.getConnection(url, username, password);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

	private static int insert(BtnDO btn) {
		Connection conn = getConn();
		int i = 0;
		String sql = "INSERT INTO security_btn (`menuId`, `name`, `code`, `menuCode`, `createtime`, `status`, `isDelete`) VALUES"
				+ "(?, ?, ?, ?, ?, ?, ?)";
		PreparedStatement pstmt;
		try {
			int id = getMenuId(btn.getMenuCode());
			System.out.println(id);
			pstmt = (PreparedStatement) conn.prepareStatement(sql);
			pstmt.setObject(1, id);
			pstmt.setObject(2, btn.getName());
			pstmt.setObject(3, btn.getCode());
			pstmt.setObject(4, btn.getMenuCode());
			pstmt.setObject(5, new Date());
			pstmt.setObject(6, 1);
			pstmt.setObject(7, 1);
			i = pstmt.executeUpdate();
			pstmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return i;
	}

	private static int getMenuId(String menuCode) {
		Connection conn = getConn();
		int id = 0;
		String sql = "select id from security_menu where code = ?";
		ResultSet rs = null;
		PreparedStatement pstmt;
		try {
			pstmt = (PreparedStatement) conn.prepareStatement(sql);
			pstmt.setObject(1, menuCode);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				id = rs.getInt(1);
			}
			pstmt.close();
			conn.close();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return id;
	}
	
	

}
